var _count;
var _currentStepIndex = 1;


//页面跳转
function methodBtn(index, method, end) {
    var fFor;
    if (end != true) {
        if (method == "back") {
            _currentStepIndex -= 1;
            if (index == 1) {
                fFor = ".for" + String.fromCharCode(index + 65);
            } else {
                fFor = ".for" + String.fromCharCode(index + 64);
            }
            $(fFor).removeClass("for-cur");
            loadFlowDiv(index-1);
            checkColor("default");
        } else if (method == "forward") {
            _currentStepIndex += 1;
            fFor = ".for" + String.fromCharCode(index + 65);
            $(fFor).addClass("for-cur");
            loadFlowDiv(index+1);
            checkColor();
        }
    } else if (end == true) {
       
    }

}

function redirectBtn(targetIndex, method, end) {
    var fFor;
    if (end != true) {
        if (method == "back") {
            for(var i=targetIndex;i<_currentStepIndex;i++) {
                fFor = ".for" + String.fromCharCode(i + 65);
                $(fFor).removeClass("for-cur");
            }
            loadFlowDiv(targetIndex);
            checkColor("default");
        } else if (method == "forward") {
            for(var i=_currentStepIndex;i<targetIndex;i++) {
                fFor = ".for" + String.fromCharCode(i + 65);
                $(fFor).addClass("for-cur");
            }

            loadFlowDiv(targetIndex);
            checkColor();
        }
    } else if (end == true) {

    }
    _currentStepIndex = targetIndex;

    if (targetIndex != 1) {
        /*非第一步的时候，显示上一步*/
        $("#btnBack").removeClass("disabled");
        $("#btnBack").show();
    } else {
        $("#btnBack").addClass("disabled");
    }
    if (targetIndex >= _count) {
        /*到最后一步时 去掉下一步 显示上一步和完成*/
        $("#btnNext").hide();
        $("#btnBack").show();
        $("#btnok").show();
    } else {
        $("#btnNext").show();
    }
}

//确定流程颜色状态
function checkColor(color) {
    if (color != "default") {
        $(".flowList.for-cur").css({ "border": "2px solid #1ABB9C"});
        $(".flowList.for-cur,.flowListBox .for-cur em").css({ "background-color": "#1ABB9C"});
        $(".flowListBox .for-cur em").css({ "border": "0px none #000" }); 
    } else {
        $this = $('.flowList:not(.for-cur)');
        $this.css({ "border": "2px solid #ccc", "background-color": "#ccc" });
        $this.children("em").css({ "background-color": "#ccc" }); 
    }
    /*让当前选中步骤变为深蓝色*/
	 var obj=$('.for-cur:last');
	 obj.css({ "border": "2px solid #34495e", "background-color": "#34495e" });
	 obj.children("em").css({ "background-color": "#34495e" }); 
}
//确定步骤的宽度占比
function fixWidth(count) {
    var part = parseInt(100 / count) + "%";
    $(".flowListBox .flowList").css("width", part);
}
//加载步骤
function loadFlow(count, steps){
  _count = count;
  var flowFor;
  var flowVar="";
  for(var i=1;i<=count;i++){
    flowFor="for"+String.fromCharCode(i+64);
    if(i==1){
      flowVar += "<div class='flowList for-cur "+flowFor +"' data-index="+i+" onclick='skipStep(this)' style='position:relative'>\n";
      flowVar += "	<em data-index="+i+" style='position:absolute;left:35%'>"+i+"</em><br/><strong data-index="+i+" style='width: auto;position: absolute; left: 0; right: 0; text-align: center;'>"+steps[i-1]+"</strong>\n";
      flowVar += "</div>\n";
    }else{
      flowVar += "<div class='flowList "+flowFor +"' data-index="+i+" onclick='skipStep(this)' style='position:relative'>\n";
      flowVar += "	<em data-index="+i+" style='position:absolute;left:35%'>"+i+"</em><br/><strong data-index="+i+" style='width: auto;position: absolute; left: 0; right: 0; text-align: center;'>"+steps[i-1]+"</strong>\n";
      flowVar += "</div>\n";
    }

  }
  $(".flowListBox").html(flowVar);
  fixWidth(count);
  loadFlowDiv(1);
  checkBtn(1,count);
  checkColor();
}
//加载内容详情
function loadFlowDiv(index){ 
 if(index==1){
     $("#contA").removeClass("contentList");
     $("#contA").siblings().addClass("contentList")
 }
 if(index==2){
     $("#contB").removeClass("contentList");
     $("#contB").siblings().addClass("contentList")
 }
 if(index==3){
     $("#contC").removeClass("contentList");
     $("#contC").siblings().addClass("contentList")
 }
 if(index==4){
     $("#contD").removeClass("contentList");
     $("#contD").siblings().addClass("contentList")
 }
 if(index==5){
     $("#contE").removeClass("contentList");
     $("#contE").siblings().addClass("contentList")
 }
 if(index==6){
 $("#contF").removeClass("contentList");
 $("#contF").siblings().addClass("contentList")
 }
}
//上一步下一步按钮点击事件
var maxstep=1;
function checkBtn(index, count) {
	
    $("#btnBack").addClass("disabled");
	 /*默认进来隐藏上一步按钮*/
	$("#btnBack").hide();	
	/*下一步点击事件*/ 
    $("#btnNext").click(function () {			 
        methodBtn(index++, 'forward', false);
		if(index>maxstep){
			maxstep=index;
		}
        if (index != 1) {
			/*非第一步的时候，显示上一步*/
            $("#btnBack").removeClass("disabled");
			$("#btnBack").show();
        }
        if (index >= count) {  
		/*到最后一步时 去掉下一步 显示上一步和完成*/
            $("#btnNext").hide();
            $("#btnBack").show();
			$("#btnok").show();
        }
        refreshBack(index);
    });
	/*上一步点击事件*/
    $("#btnBack").click(function () {
        if (refreshBack(index) > 1) {
            methodBtn(index--, 'back', false);
			$("#btnNext").show();
            if (index == 1) {
				/*如果当前为第一步 则给上一步添加disabled属性*/
                $("#btnBack").addClass("disabled");
            } 
			/* for(var num=1;num<=maxstep;num++){
				 if(num==maxstep+1){
					  $(".flowList.for-cur").css({ "border": "2px solid #1ABB9C"});
        			  $(".flowList.for-cur,.flowListBox .for-cur em").css({ "background-color": "#1ABB9C"});
					 }
				 }*/
        }
    });
}
/*上一步*/
function refreshBack(index) {
    return index;
}

function skipStep(obj) {
    var _targetStepIndex = Number($(obj).attr("data-index"));
    if(_currentStepIndex == _targetStepIndex) {
        return;
    } else if(_targetStepIndex > _currentStepIndex) {
        redirectBtn(_targetStepIndex, 'forward', false);
    } else if(_targetStepIndex < _currentStepIndex) {
        redirectBtn(_targetStepIndex, 'back', false);
    }
}